﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using System.Text;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;

public partial class BasicBase_Vehicle_VehicleDevices : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCodeItemList(DeviceClassList, "ZBQC010000");
            BindDevice();
            BindDeversInfo();
            ListPageContral1.SetFirstPage();
            BindVehicleDeviceds(0);

        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindVehicleDeviceds(PageIndex);
    }

    private void BindCodeItemList(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList("ParentID='" + parentID + "' ORDER BY SeqID");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList(ddl, item.ItemID);
        }
    }

    protected void DeviceClassList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDevice();
        BindDeversInfo();
    }

    public string GetCodeItem(string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass);
        return info != null ? info.ItemName : "";
    }


    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            #region 赋值
            VehicleDeviceInfo info = e.Row.DataItem as VehicleDeviceInfo;
            DeviceInfo deviceInfo = info.DeviceInfo;
            Label DeviceName = e.Row.FindControl("DeviceName") as Label;
            DeviceName.Text = deviceInfo.DeviceName;

            Label DeviceClass = e.Row.FindControl("DeviceClass") as Label;
            DeviceClass.Text = GetCodeItem(deviceInfo.DeviceClass);

            Label State = e.Row.FindControl("State") as Label;
            State.Text = GetCodeItem(deviceInfo.State);

            #endregion
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (VehicleDevice.GetDetail(new Guid(Request.QueryString["id"]), new Guid(ddlDevice.SelectedValue)) == null)
        {
            //数量的限制
            if (Convert.ToInt32(DeviceCount.Text) == 0)
            {
                Prompt.Text = "提示：个数不能为0！";
                return;
            }
            DeviceInfo deviceinfo = Device.GetDevice(new Guid(ddlDevice.SelectedValue));
            if (Convert.ToInt32(DeviceCount.Text) > deviceinfo.DeviceCount)
            {
                Prompt.Text = "提示：个数不能大于此器材的总数！";
                return;
            }

            VehicleDeviceInfo info = new VehicleDeviceInfo();
            info.DeviceID = new Guid(ddlDevice.SelectedValue);
            info.VehicleID = new Guid(Request.QueryString["id"]);
            info.DeviceCount = Convert.ToInt32(DeviceCount.Text);

            if (VehicleDevice.Insert(info))
            {
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "添加新记录";
                log.ItemID = info.DeviceID.ToString() + "/" + info.VehicleID.ToString();
                log.ItemName = "VihicleDevices";
                log.ItemFullUrl = "VihicleDevices.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);

                Prompt.Text = "提示：添加成功！";
                ListPageContral1.SetFirstPage();
                BindVehicleDeviceds(0);
            }
            else
            {
                Prompt.Text = "提示：添加失败！";
            }
        }
        else
        {
            Prompt.Text = "提示：此装备已是随车器材！";
        }
    }

    void BindDevice()
    {
        Guid Vid = new Guid(Request.QueryString["id"]);
        VehicleInfo info = Vehicle.GetDetail(Vid);
        StringBuilder selectCommandText = new StringBuilder();
        selectCommandText.Append(" departmentid='" + info.DepartmentID + "'");
        CodeItemInfo item = CodeItem.GetCodeItem(DeviceClassList.SelectedValue);
        if (item.ItemLevel == 0)
            selectCommandText.Append(" and left(deviceclass,6)='" + DeviceClassList.SelectedValue.Substring(0, 6) + "'");
        if (item.ItemLevel == 1)
            selectCommandText.Append(" and left(deviceclass,8)='" + DeviceClassList.SelectedValue.Substring(0, 8) + "'");
        if (item.ItemLevel == 2)
            selectCommandText.Append(" and deviceclass='" + DeviceClassList.SelectedValue + "'");
        List<DeviceInfo> list = Device.GetList(selectCommandText.ToString());
        ddlDevice.DataTextField = "DeviceName";
        ddlDevice.DataValueField = "DeviceID";
        ddlDevice.DataSource = list;
        ddlDevice.DataBind();
    }
    protected void ddlDevice_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDeversInfo();
    }

    private void BindDeversInfo()
    {
        if (ddlDevice.SelectedValue != "")
        {
            DeviceInfo info = Device.GetDevice(new Guid(ddlDevice.SelectedValue));
            lblDetail.Visible = true;
            lblDetail.Text = "<a href=javascript:OpenWin('../DeviceView.aspx?DeviceID=" + ddlDevice.SelectedValue + "');>查看</a>";
            DeviceCount.Text = info.DeviceCount.ToString();
            Unit.Visible = true;
            Unit.Text = GetCodeItem(info.UnitName);
        }
        else
        {
            Unit.Visible = false;
            lblDetail.Visible = false;
            DeviceCount.Text = "";
        }
    }

    void BindVehicleDeviceds(int PageIndex)
    {
        ListPageContral1.ItemCount = VehicleDevice.GetCount(Request.QueryString["id"]);
        List<VehicleDeviceInfo> list = VehicleDevice.GetList(ListPageContral1.PageSize, PageIndex, Request.QueryString["id"]);
        GridView1.DataSource = list;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            VehicleDevice.Delete(new Guid(Request.QueryString["id"]), new Guid(e.CommandArgument.ToString()));
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "删除随车器材";
            log.ItemID = Request.QueryString["id"].ToString() + "/" + e.CommandArgument.ToString();
            log.ItemName = "VihicleDevices";
            log.ItemFullUrl = "VihicleDevices.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
            ListPageContral1.SetFirstPage();
            BindVehicleDeviceds(0);
            Prompt.Text = "提示：删除成功！";
            BindVehicleDeviceds(ListPageContral1.CurrentPageIndex);
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindVehicleDeviceds(ListPageContral1.CurrentPageIndex);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string DiviceID = GridView1.DataKeys[e.RowIndex][0].ToString();
        string VehicleID = Request.QueryString["id"];
        TextBox txtDeviceCount = GridView1.Rows[e.RowIndex].FindControl("txtDeviceCount") as TextBox;
        //数量的限制
        if (Convert.ToInt32(txtDeviceCount.Text) == 0)
        {
            Prompt.Text = "提示：个数不能为0！";
            return;
        }
        DeviceInfo deviceinfo = Device.GetDevice(new Guid(DiviceID));
        if (Convert.ToInt32(txtDeviceCount.Text) > deviceinfo.DeviceCount)
        {
            Prompt.Text = "提示：个数不能大于此器材的总数！";
            return;
        }

        VehicleDeviceInfo info = VehicleDevice.GetDetail(new Guid(VehicleID), new Guid(DiviceID));
        info.DeviceCount = Convert.ToInt32(txtDeviceCount.Text);
        if (VehicleDevice.Update(info))
        {
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "修改随车器材";
            log.ItemID = VehicleID + "/" + DiviceID;
            log.ItemName = "VihicleDevices";
            log.ItemFullUrl = "VihicleDevices.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
            ListPageContral1.SetFirstPage();
            BindVehicleDeviceds(0);
            Prompt.Text = "提示：修改成功！";
            GridView1.EditIndex = -1;
            BindVehicleDeviceds(ListPageContral1.CurrentPageIndex);
        }
        else
        {
            Prompt.Text = "提示：修改失败！";
            GridView1.EditIndex = -1;
            BindVehicleDeviceds(ListPageContral1.CurrentPageIndex);
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindVehicleDeviceds(ListPageContral1.CurrentPageIndex);
    }
}
